import { LEVEL_ID, LEVEL_INT } from './area'
import localStore from '@/common/localStore'
import { clearDataCache } from '@/assets/js/common'

const genMapArea = area => ({
  level: area.level,
  id: area.id,
  areaId: area.id,
  name: area.name,
  features: null,
  list: null,
  generalOverview: null,
  sleepOverview: null,
  sportAndSleep: null,
  attendOverview: null,
  attend: null,
  check: null,
  avgCheck: null,
  avgGardenCheck: null,
  gradeSportAndSleep: null
})

const mutations = {
  init () {
    this.commit('loadCrumbs')
    this.commit('loadUser')
    this.commit('loadWarnPercent')
  },
  login (_, user) {
    if (this.getters.isLogin) {
      this.commit('logout')
    }
    this.commit('setUser', user)
    this.commit('persistUser')
    this.commit('persistCrumbs')
  },
  logout () {
    clearDataCache()
    this.commit('setUser', null)
    this.commit('persistUser')
    this.commit('persistCrumbs')
  },
  loadUser () {
    this.commit('setUser', localStore.get('user'))
  },
  setUser (state, user) {
    state.user = user
    state.area = user === null ? null : user.area
    state.areaLevel = user === null ? null : user.areaLevel

    if (user) {
      if (!state.crumbs) {
        this.commit('genCrumbs')
      }
    } else {
      this.commit('setCrumbs', null)
    }
  },
  persistUser (state) {
    localStore.set('user', state.user)
  },
  fetchUsers (state, users) {
    state.users = users
  },
  fetchCityMap (state, map) {
    state.cityMap = map
  },
  fetchProvinces (state, provinces) {
    state.provinces = provinces
  },
  fetchMapFeatures (state, features) {
    state.mapArea.features = features
  },
  fetchGeneralOverview (state, generalOverview) {
    state.mapArea.generalOverview = generalOverview
  },
  fetchSleepOverview (state, sleepOverview) {
    state.mapArea.sleepOverview = sleepOverview
  },
  fetchSportAndSleep (state, sportAndSleep) {
    state.mapArea.sportAndSleep = sportAndSleep
  },
  fetchAttendOverview (state, attendOverview) {
    state.mapArea.attendOverview = attendOverview
  },
  fetchAttend (state, attend) {
    state.mapArea.attend = attend
  },
  fetchCheck (state, check) {
    state.mapArea.check = check
  },
  fetchAvgGardenCheck (state, avgGardenCheck) {
    state.mapArea.avgGardenCheck = avgGardenCheck
  },
  fetchAvgCheck (state, avgCheck) {
    state.mapArea.avgCheck = avgCheck
  },
  fetchGradeSportAndSleep (state, sportAndSleep) {
    state.mapArea.gradeSportAndSleep = sportAndSleep
  },
  genCrumbs (state) {
    let crumbs = []
    let level = state.user.level
    crumbs.push({
      id: state.user[LEVEL_ID['省级']],
      name: state.user.province,
      level: '省级'
    })
    if (LEVEL_INT[level] > LEVEL_INT['省级']) {
      crumbs.push({
        id: state.user[LEVEL_ID['市级']],
        name: state.user.city,
        level: '市级'
      })
    }
    if (LEVEL_INT[level] > LEVEL_INT['市级']) {
      crumbs.push({
        id: state.user[LEVEL_ID['区级']],
        name: state.user.district,
        level: '区级'
      })
    }
    if (LEVEL_INT[level] > LEVEL_INT['区级']) {
      crumbs.push({
        id: state.user[LEVEL_ID['园级']],
        name: state.user.garden,
        level: '园级'
      })
    }
    this.commit('setCrumbs', crumbs)
  },
  fillMapArea (state) {
    if (!state.crumbs) {
      state.mapArea = null
    } else {
      let area = state.crumbs[state.crumbs.length - 1]
      state.mapArea = genMapArea(area)
    }
  },
  loadCrumbs () {
    this.commit('setCrumbs', localStore.get('crumbs'))
  },
  setCrumbs (state, crumbs) {
    state.crumbs = crumbs
    this.commit('fillMapArea')
  },
  pushCrumb (state, crumb) {
    this.commit('setCrumbs', state.crumbs.concat([crumb]))
    this.commit('persistCrumbs')
  },
  popCrumb (state) {
    this.commit('setCrumbs', state.crumbs.filter(c => state.crumbs.indexOf(c) !== (state.crumbs.length - 1)))
    this.commit('persistCrumbs')
  },
  persistCrumbs (state) {
    localStore.set('crumbs', state.crumbs)
  },
  loadWarnPercent () {
    let percent = localStore.get('warnPercent')
    if (percent) {
      this.commit('setWarnPercent', percent)
    }
  },
  setWarnPercent (state, percent) {
    state.warnPercent = percent
  },
  persistWarn (state) {
    localStore.set('warnPercent', state.warnPercent)
  },
  setMapTag (state, tag) {
    state.mapTag = tag
  },
  changeAreaIdList (state, areaIdList) {
    state.areaIdList = areaIdList
  },
  changeGardenIdList (state, gardenIdList) {
    state.gardenIdList = gardenIdList
  }
}

export default mutations
